package com.qianniu.napi.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static Date toDate(String dateStr){
        return toDate(dateStr,"yyyy-MM-dd HH:mm:ss");
    }


    public static Date toDate(String dateStr,String format){

//      String dateStr = "2012-12-31 00:00:00";
        Date date = new Date();

        DateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(dateStr);
        }catch (Exception e){
            logger.error("toDate error",e);
        }
        return date;
    }


    public static String toDateString(String format){

//      String dateStr = "2012-12-31 00:00:00";
        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat(format);

        try {
            return sdf.format(date);
        }catch (Exception e){
            logger.error("toDateString error",e);
        }
        return null;
    }

    public static String toDateString(Date date,String format){

        if(date==null)return "";
//      String dateStr = "2012-12-31 00:00:00";
//        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat(format);

        try {
            return sdf.format(date);
        }catch (Exception e){
            logger.error("toDateString error",e);
        }
        return null;
    }

    public static String toDateString(String timeStamp,String format){

        Date date = new Date();
        date.setTime(Long.valueOf(timeStamp));
        DateFormat sdf = new SimpleDateFormat(format);

        try {
            return sdf.format(date);
        }catch (Exception e){
            logger.error("toDateString error",e);
        }
        return null;
    }

    public static String getToday(){
        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getToday(Date date) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getToday(String fmt) {
        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.format(date);
    }

    public static String getShortToday(){
        Date date = new Date();
        DateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        return sdf.format(date);
    }

    // 获取当前时间戳，精确到毫秒
    public static long getTime(){
        Date date = new Date();
        return date.getTime();
    }

    /**
     * 设置微信二维码失效时间，并返回具体失效的时间点
     * @param expire 二维码的有效时间，单位是毫秒
     * @return
     */
    public static String getOrderExpireTime(Long expire){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date now = new Date();
        Date afterDate = new Date(now .getTime() + expire);
        return sdf.format(afterDate );
    }

    // 获取当前小时
    public static long getHour(){

        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.HOUR_OF_DAY);
    }

    // 获取当前分钟
    public static long getMinute(){

        Calendar cal = Calendar.getInstance();
        return cal.get(Calendar.MINUTE);
    }

    /**
     * 在日期上增加数个整天
     */
    public static Date addDay(Date date, int n)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, n);
        return cal.getTime();
    }

    public static String addToday(int days) {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, days);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public static String addToday(String stateday,int days) {
        try {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(stateday);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DAY_OF_YEAR, days);

            return sdf.format(cal.getTime());
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 在日期上增加数个整月
     */
    public static Date addMonth(Date date, int n)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, n);
        return cal.getTime();
    }

    public static String getLastDayOfMonth(String year, String month)
    {
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        // 月，因为Calendar里的月是从0开始，所以要-1
        // cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);
        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);
        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
    }

    public static void main(String[] args){

        Calendar cal = Calendar.getInstance();

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int date = cal.get(Calendar.DATE);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);

//        System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);

        System.out.println("today yyyy-M-d="+DateUtil.toDateString(new Date(),"yyyy-M-d"));


//        String txt = "你好，朋友。曾经沧海难为水；除却巫山不是云";
//
//        String[] arr = txt.split(RegexUtil.REGEX_TAIL_CHAR.pattern());
//        for (String item:arr){
//            System.out.println("item="+item);
//        }

        System.out.println("hour="+DateUtil.getHour()+" minute="+DateUtil.getMinute());
    }

}
